<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\CategoryAttributeValue;
use App\Commands\OperationLogCommand;
use App\Models\ProductAttributeValue;
use App\Models\ProductAttribute;

class CategoryAttributeValueController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request,CategoryAttributeValue $categoryAttributeValue)
    {
        //dd($request->all());
        $cat_id = $request->get('cat_id');
        $category_attribute_id = $request->get('category_attribute_id');
        return view('admin.category.addAttributeValue',compact(['cat_id','category_attribute_id', 'categoryAttributeValue']));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request,CategoryAttributeValue $categoryAttributeValue)
    {

        $param = $request->all();
        $back_url = $request->get('back_url');

//        $categoryAttributeValueArr = $categoryAttributeValue->lists('attribute_value')->toArray();
//        $is_categoryAttributeValue = in_array($request->get('attribute_value'),$categoryAttributeValueArr);
//        if($is_categoryAttributeValue){
//            return redirect::back()->withInput()->with('message','该属性值已存在');
//        }

        $categoryAttributeValue = $categoryAttributeValue->create($param);
        $this->dispatch(
            new OperationLogCommand('添加分类属性值','CategoryAttributeValue','id:'.$categoryAttributeValue->id)
        );
        return Redirect($back_url);

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $result = ['status'=>1, 'message'=>'', 'data'=>''];
        $categoryAttributeValue = CategoryAttributeValue::find($id);
        //dd($categoryAttribute);
        $categoryAttributeValue = $categoryAttributeValue->update($request->all());
        $this->dispatch(
            new OperationLogCommand('修改属性值','CategoryAttributeValue','id:'.$id)
        );
        if($categoryAttributeValue){
            $result['data']=$request->get('name');
        }else{
            $result['status']=0;
            $result['message']='修改失败';
        }
        return $result;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
    public function categoryAttributeValue(Request $request){
        $cat_id = $request->get('cat_id');
        $category_attribute_id = $request->get('category_attribute_id');
        $categoryAttributeValue = CategoryAttributeValue::where('category_attribute_id',$category_attribute_id)->orderBy('id','desc')->paginate(10);
        return view('admin.category.attributeValue',compact(['category_attribute_id','cat_id','categoryAttributeValue']));

    }

    /**
     * 删除分类下的属性值
     *
     * @param  int  $cat_id：分类id
     * @param  int  $id：属性id
     * @return \Illuminate\Http\Response
     */
    public function delCategoryAttributeValue(Request $request){
        $result = ['status'=>1, 'message'=>'', 'data'=>''];
        $id = $request->get('id');
        $cat_id = $request->get('cat_id');
        $category_attribute_id = $request->get('category_attribute_id');

        /* 删除的限制条件*/
        $productAttributeValue = ProductAttributeValue::where('attribute_value',$id)->get()->toArray();
        //dd($productAttributeValue);
        if($productAttributeValue){
            $result['status']=0;
            $result['message']='该属性值已被使用，不能删除！';
        }else{
            $categoryAttributeValue = CategoryAttributeValue::where('category_attribute_id',$category_attribute_id)->where( 'id', $id)->delete();
        }
        $this->dispatch(
            new OperationLogCommand('删除属性值','CategoryAttribute','id:'.$id)
        );
        return $result;
    }//end func categoryBrand()



}
